package com.cloud.pole.shop.service.goods.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ace.cache.api.CacheAPI;
import com.cloud.pole.api.user.UserCollectionApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.BeanUtils;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.feign.IUserCollectionService;
import com.cloud.pole.shop.biz.goods.ShopGoodsAttrBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsGroupGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsImgBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsSpecBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsValueBiz;
import com.cloud.pole.shop.biz.order.ShopOrderShoppingCartBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsPayTypeBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsPayTypeStageNumBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAdmin;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAttr;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAttrValue;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroup;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroupGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsImg;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsModelAttr;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsModelAttrValue;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsSpec;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsValue;
import com.cloud.pole.shop.model.entity.order.ShopOrderShoppingCart;
import com.cloud.pole.shop.model.entity.shops.ShopShopsPayType;
import com.cloud.pole.shop.model.entity.shops.ShopShopsPayTypeStageNum;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsAttrVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsSpecVo;
import com.cloud.pole.shop.service.goods.GoodsAttrService;
import com.cloud.pole.shop.service.goods.GoodsAttrValueService;
import com.cloud.pole.shop.service.goods.GoodsModelAttrService;
import com.cloud.pole.shop.service.goods.GoodsModelAttrValueService;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年1月30日
*cloud-user
*author:frank-chen
*/
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private ShopOrderShoppingCartBiz shopOrderShoppingCartBiz;

	@Autowired
	private IUserCollectionService userCollectionService;

	@Autowired
	private ShopGoodsBiz shopGoodsBiz;

	@Autowired
	private ShopGoodsSpecBiz shopGoodsSpecBiz;

	@Autowired
	private ShopShopsBiz shopShopsBiz;

	@Autowired
	private ShopGoodsValueBiz shopGoodsValueBiz;

	@Autowired
	private ShopGoodsImgBiz shopGoodsImgBiz;

	@Autowired
	private ShopShopsPayTypeBiz shopShopsPayTypeBiz;

	@Autowired
	private ShopShopsPayTypeStageNumBiz shopShopsPayTypeStageNumBiz;

	@Autowired
	private ShopGoodsGroupGoodsBiz shopGoodsGroupGoodsBiz;

	@Autowired
	private ShopGoodsAttrBiz shopGoodsAttrBiz;

	@Autowired
	private GoodsModelAttrService goodsModelAttrService;

	@Autowired
	private GoodsAttrService goodsAttrService;

	@Autowired
	private GoodsModelAttrValueService goodsModelAttrValueService;

	@Autowired
	private GoodsAttrValueService goodsAttrValueService;
	
	@Autowired
    private CacheAPI cacheAPI;

	@Override
	public GoodsDetailVo getGoodsDetail(String token, String goodsId, String specId, String recommandMarketCode)
			throws Exception {
		if(StringUtils.isNoneBlank(token)){
			cacheAPI.set("recommandMarketCode-" + token, recommandMarketCode, 1440);
		}
		
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
		// 购物车数量
		long cartCount = 0;
		// 收藏标志
		int collectStatus = 0;
		// 选中规格
		int specIndex = 0;
		// 是否登录
		if (!StringUtils.isEmpty(token)) {
			IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
			// 购物车商品数量
			ShopOrderShoppingCart shopOrderShoppingCart = new ShopOrderShoppingCart();
			shopOrderShoppingCart.setUserId(Long.parseLong(jwtInfo.getId()));
			cartCount = shopOrderShoppingCartBiz.selectCount(shopOrderShoppingCart);
			// 查询此商品是否收藏
			ApiRestResponse<UserCollectionApi> userCollectionResponse = userCollectionService.getUserCollection(
					Long.parseLong(jwtInfo.getId()), UserCollectionApi.OBJECT_TYPE_ID_GOODS, goodsId);
			if (userCollectionResponse.getStatus() != CommonConstants.STATUS_OK_CODE) {
				throw new BaseException(userCollectionResponse.getStatus(), userCollectionResponse.getMessage());
			}
			UserCollectionApi userCollectionVo = userCollectionResponse.getData();
			if (userCollectionVo != null && StringUtils.equals(userCollectionVo.getObjectId(), goodsId)) {
				collectStatus = 1;
			}
		}

		// 根据商品ID查出商品
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(Integer.parseInt(goodsId));
//		if (!checkShoppingGoods(shopGoods)) {
//			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
//		}

		// 商品规格
		List<ShopGoodsSpec> shopGoodsSpecList = shopGoodsSpecBiz.selectListByGoodsId(Integer.parseInt(goodsId));
		List<ShopGoodsSpecVo> shopGoodsSpecVoList = new ArrayList<ShopGoodsSpecVo>();
		for (int i = 0; i < shopGoodsSpecList.size(); i++) {
			ShopGoodsSpecVo shopGoodsSpecVo = new ShopGoodsSpecVo();
			org.springframework.beans.BeanUtils.copyProperties(shopGoodsSpecList.get(i), shopGoodsSpecVo);
			shopGoodsSpecVo.setS1("0");
			shopGoodsSpecVo.setS2("0");
			shopGoodsSpecVo.setS3("0");
			if (StringUtils.isNotEmpty(shopGoodsSpecVo.getGoodsAttrValueIds())) {
				String[] ids = shopGoodsSpecVo.getGoodsAttrValueIds().split(",");
				shopGoodsSpecVo.setS1(ids[0]);
				if (ids.length > 1) {
					shopGoodsSpecVo.setS2(ids[1]);
				}
				if (ids.length > 2) {
					shopGoodsSpecVo.setS3(ids[2]);
				}
			}
			shopGoodsSpecVoList.add(shopGoodsSpecVo);
		}

		if (shopGoodsSpecList == null || shopGoodsSpecList.size() == 0) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品规格不存在");
		}

		// 商品对应模型属性
		List<ShopGoodsValue> shopGoodsValueList = shopGoodsValueBiz.selectListByGoodsId(Integer.parseInt(goodsId));

		if (!StringUtils.isEmpty(specId)) {
			for (int i = 0; i < shopGoodsSpecList.size(); i++) {
				ShopGoodsSpec tempSpect = shopGoodsSpecList.get(i);
				if (tempSpect.getSpceId().intValue() == Integer.parseInt(specId)) {
					specIndex = i;
					break;
				}
			}
		}
		// 查询店铺信息
		ShopShopsWithBLOBs shopShops = shopShopsBiz.selectById(shopGoods.getShopId());

		// 根据商品ID查出图片
		List<ShopGoodsImg> shopGoodsImgList = shopGoodsImgBiz.selectListByGoodsId(Integer.parseInt(goodsId));

		// 商品对应规格
		List<ShopGoodsAttrVo> shopGoodsAttrList = shopGoodsAttrBiz.selectListByGoodsId(Integer.parseInt(goodsId));
		for (int i = 0; i < shopGoodsAttrList.size(); i++) {
			shopGoodsAttrList.get(i).setKeyStr("s" + (i + 1));
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shopId", shopGoods.getShopId());
		List<ShopShopsPayTypeStageNum> StageNums = shopShopsPayTypeStageNumBiz.selectListByMap(map);

		goodsDetailVo.setCartCount(cartCount);
		goodsDetailVo.setCollectStatus(collectStatus);
		goodsDetailVo.setShopGoods(shopGoods);
		goodsDetailVo.setShopGoodsImgList(shopGoodsImgList);
		goodsDetailVo.setShopGoodsSpecList(shopGoodsSpecVoList);
		goodsDetailVo.setShopGoodsValueList(shopGoodsValueList);
		goodsDetailVo.setShopId(shopShops.getShopId());
		goodsDetailVo.setShopName(shopShops.getShopName());
		goodsDetailVo.setSpecIndex(specIndex);
		goodsDetailVo.setShopGoodsAttrList(shopGoodsAttrList);
		if (!StageNums.isEmpty()) {
			goodsDetailVo.setStageNum(StageNums.get(0).getStageNum());
		} else {
			goodsDetailVo.setStageNum(1);
		}
		return goodsDetailVo;
	}

	@Override
	public boolean addCollection(String token, String goodsId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(Integer.parseInt(goodsId));
		if (!checkShoppingGoods(shopGoods)) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");

		}
		ApiRestResponse<Boolean> response = userCollectionService.addUserCollection(Long.parseLong(jwtInfo.getId()),
				jwtInfo.getUniqueName(), "", UserCollectionApi.OBJECT_TYPE_ID_GOODS, goodsId, "");
		if (response.getStatus() != CommonConstants.STATUS_OK_CODE) {
			throw new BaseException(response.getStatus(), response.getMessage());
		}
		return true;
	}

	@Override
	public boolean cancelCollection(String token, String goodsId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(Integer.parseInt(goodsId));
		if (!checkShoppingGoods(shopGoods)) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");

		}
		ApiRestResponse<Boolean> response = userCollectionService.cancelUserCollection(Long.parseLong(jwtInfo.getId()),
				UserCollectionApi.OBJECT_TYPE_ID_GOODS, goodsId);
		if (response.getStatus() != CommonConstants.STATUS_OK_CODE) {
			throw new BaseException(response.getStatus(), response.getMessage());
		}
		return true;
	}

	@Override
	public boolean checkShoppingGoods(ShopGoods shopGoods) throws Exception {
		// TODO Auto-generated method stub
		// 审核通过
		// 上架状态:(1上架,-1下架)
		// 未删除
		// 显示状态:(-1屏蔽,1显示)
		if (shopGoods == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品不存在");
		}
		if (shopGoods.getCheckStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		}
		if (shopGoods.getStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		}
		if (shopGoods.getDeleteStatus() != -1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		}
		if (shopGoods.getShowStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		}
		return true;
	}

	@Override
	public boolean checkGoodsSpec(ShopGoodsSpec shopGoodsSpec) throws Exception {
		// TODO Auto-generated method stub
		if (shopGoodsSpec == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "规格无效");
		}
		return true;
	}

	@Override
	public PageInfo<ShopGoodsAdmin> selectPageShopGoodsAdminList(Map<String, Object> map, Integer pageNo,
			Integer pageSize) throws Exception {
		PageHelper.startPage(pageNo, pageSize);
		List<ShopGoodsAdmin> list = shopGoodsBiz.selectPageShopGoodsAdminList(map);
		PageInfo<ShopGoodsAdmin> page = new PageInfo<ShopGoodsAdmin>(list);
		return page;
	}

	@Override
	@Transactional
	public void saveGoods(String token, Map map) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);

		// Integer oringinalPrice = new BigDecimal(map.getOrDefault("", "0").toString())
		// .multiply(new BigDecimal(100)).intValue();
		// Integer currentPrice = new BigDecimal(map.getOrDefault("currentPrice",
		// "0").toString())
		// .multiply(new BigDecimal(100)).intValue();

		List<Object> oringinalPrice = (List<Object>) map.get("price");
		List<Integer> oringinalPriceInteger = new ArrayList<Integer>();
		Integer oringinalPriceEs = null;
		Integer oringinalPriceGoods = null;
		if (oringinalPrice != null || oringinalPrice.size() != 0) {
			for (Object str : oringinalPrice) {
				double parseDouble = Double.parseDouble(str.toString());
				oringinalPriceEs = (int) (parseDouble * 100);
				oringinalPriceInteger.add(oringinalPriceEs);
			}
			oringinalPriceGoods = oringinalPriceInteger.get(0);
			for (int i = 0; i < oringinalPriceInteger.size(); i++) {
				if (oringinalPriceGoods > oringinalPriceInteger.get(i)) {
					oringinalPriceGoods = oringinalPriceInteger.get(i);
				}
			}
		}
		List<Object> currentPrice = (List<Object>) map.get("memberPrice");
		List<Integer> currentPriceInteger = new ArrayList<Integer>();
		Integer currentPriceEs = null;
		Integer currentPriceGoods = null;
		if (currentPrice != null || currentPrice.size() != 0) {
			for (Object integer : currentPrice) {
				double parseDouble = Double.parseDouble(integer.toString());
				currentPriceEs = (int) (parseDouble * 100);
				currentPriceInteger.add(currentPriceEs);
			}
			currentPriceGoods = currentPriceInteger.get(0);
			for (int i = 0; i < currentPriceInteger.size(); i++) {
				if (currentPriceGoods > currentPriceInteger.get(i)) {
					currentPriceGoods = currentPriceInteger.get(i);
				}
			}
		}

		List<Object> menchantPrice = (List<Object>) map.get("menchantPrice");
		List<Integer> menchantPriceInteger = new ArrayList<Integer>();
		Integer menchantPriceEs = null;
		if (menchantPrice != null || menchantPrice.size() != 0) {
			for (Object integer : menchantPrice) {
				double parseDouble = Double.parseDouble(integer.toString());
				menchantPriceEs = (int) (parseDouble * 100);
				menchantPriceInteger.add(menchantPriceEs);
			}
		}

		ShopGoods goods = (ShopGoods) BeanUtils.mapToObject(map, ShopGoods.class);
		List<String> imgsList = (List<String>) map.get("goodImgs");
		String goodsNature = (String) map.get("shopGrouptionsValShopType");
		// 暂时设置为有效
		goods.setCheckStatus(1);
		// if(jwtInfo.getType() == JWTInfo.USER_TYPE_ADMIN) {
		// //goods.setShopId(1);
		// }
		goods.setSalesPromotion((String) map.get("salesPromotion"));
		goods.setCurrentPrice(currentPriceGoods); // 现价
		goods.setOringinalPrice(oringinalPriceGoods); // 原价
		goods.setMemberPrice(currentPriceGoods);
		goods.setStatus(Integer.parseInt(map.get("status").toString()));
		goods.setStockStatus(1);
		goods.setDeleteStatus(-1);
		goods.setGoodsNature(Integer.valueOf(goodsNature));
		// goods.setStatus(1);
		goods.setShowStatus(1);
		goods.setRecommendStatus(1);
		goods.setTitleImage(imgsList.get(0));
		// 设置商户号
		ShopShopsWithBLOBs shopShops = shopShopsBiz.selectById(goods.getShopId());
		goods.setMerchantId(shopShops.getMerchantId());
		goods.setCreateUserId(jwtInfo.getId());
		goods.setCreateTime(new Date());

		shopGoodsBiz.insertSelective(goods);

		String modelName = (String) map.get("modelAttrId");
		String modelNames = (String) map.get("modelAttrIde");
		String modelNamees = (String) map.get("modelAttrIdes");
		ShopGoodsModelAttr shopGoodsModelAttr = new ShopGoodsModelAttr();
		ShopGoodsModelAttr shopGoodsModelAttr_a = new ShopGoodsModelAttr();
		ShopGoodsModelAttr shopGoodsModelAttr_b = new ShopGoodsModelAttr();

		ShopGoodsAttr shopGoodsAttr = new ShopGoodsAttr();
		ShopGoodsAttr shopGoodsAttr_a = new ShopGoodsAttr();
		ShopGoodsAttr shopGoodsAttr_b = new ShopGoodsAttr();
		List<ShopGoodsAttr> ShopGoodsAttrList = new ArrayList<ShopGoodsAttr>();
		if (null != modelName && modelName.length() != 0) {
			shopGoodsModelAttr = goodsModelAttrService.selectByName(modelName,goods.getShopId());
			shopGoodsAttr.setGoodsId(goods.getGoodsId());
			shopGoodsAttr.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
			shopGoodsAttr.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
			shopGoodsAttr.setShopId(shopGoodsModelAttr.getShopId());
			shopGoodsAttr.setCreateTime(new Date());
			shopGoodsAttr.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr);
		}
		if (null != modelNames && modelNames.length() != 0) {
			shopGoodsModelAttr_a = goodsModelAttrService.selectByName(modelNames,goods.getShopId());
			shopGoodsAttr_a.setGoodsId(goods.getGoodsId());
			shopGoodsAttr_a.setModelAttrId(shopGoodsModelAttr_a.getModelAttrId());
			shopGoodsAttr_a.setModelAttrName(shopGoodsModelAttr_a.getModelAttrName());
			shopGoodsAttr_a.setShopId(shopGoodsModelAttr_a.getShopId());
			shopGoodsAttr_a.setCreateTime(new Date());
			shopGoodsAttr_a.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr_a.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr_a);
		}
		if (null != modelNamees && modelNamees.length() != 0) {
			shopGoodsModelAttr_b = goodsModelAttrService.selectByName(modelNamees,goods.getShopId());
			shopGoodsAttr_b.setGoodsId(goods.getGoodsId());
			shopGoodsAttr_b.setModelAttrId(shopGoodsModelAttr_b.getModelAttrId());
			shopGoodsAttr_b.setModelAttrName(shopGoodsModelAttr_b.getModelAttrName());
			shopGoodsAttr_b.setShopId(shopGoodsModelAttr_b.getShopId());
			shopGoodsAttr_b.setCreateTime(new Date());
			shopGoodsAttr_b.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr_b.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr_b);
		}

		if (ShopGoodsAttrList != null || ShopGoodsAttrList.size() != 0) {
			for (ShopGoodsAttr goodsAttr : ShopGoodsAttrList) {
				goodsAttrService.insertGoodsAttr(goodsAttr);
			}
		}

		List<String> attrValueIdS = (List<String>) map.get("attrValueId");
		List<String> attrValueIdeS = (List<String>) map.get("attrValueIde");
		List<String> attrValueIdesS = (List<String>) map.get("attrValueIdes");

		List<String> imgsSpecList = (List<String>) map.get("goodsSpecImgs");

		List<ShopGoodsModelAttrValue> selectByName = null;
		List<ShopGoodsModelAttrValue> selectByName_a = null;
		List<ShopGoodsModelAttrValue> selectByName_b = null;
		
		List<ShopGoodsAttrValue> selectByGoodsAttrVal=null;
		List<ShopGoodsAttrValue> selectByGoodsAttrVal_a=null;
		List<ShopGoodsAttrValue> selectByGoodsAttrVal_b=null;
		
		List<Integer> goodsAttrVal =new ArrayList<Integer>();
		List<Integer> goodsAttrVal_a =new ArrayList<Integer>();
		List<Integer> goodsAttrVal_b =new ArrayList<Integer>();
		
		List<String> goodsAttrValName=new ArrayList<String>();
		List<String> goodsAttrValName_a=new ArrayList<String>();
		List<String> goodsAttrValName_b=new ArrayList<String>();
		
		if ((null != modelName && modelName.length() != 0) && (attrValueIdS != null || attrValueIdS.size() != 0)
				&& (imgsSpecList != null && imgsSpecList.size() != 0)) { // 有图片
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdS.size(); i++) {
				attrValueNameS.add(attrValueIdS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName = goodsModelAttrValueService.selectByName(attrValueNameS,goods.getShopId());
			}
			int i = 0;
			if (selectByName != null || selectByName.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					shopGoodsAttrValue.setImageUrl(imgsSpecList.get(i));
					i++;
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrVal.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName.add(goodsModelAttr.getAttrValueName());
				}
			}

		} else if ((null != modelName && modelName.length() != 0) && (attrValueIdS != null || attrValueIdS.size() != 0)
				&& (imgsSpecList == null || imgsSpecList.size() == 0)) { // 没有图片
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdS.size(); i++) {
				attrValueNameS.add(attrValueIdS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName = goodsModelAttrValueService.selectByName(attrValueNameS,goods.getShopId());
			}
			if (selectByName != null || selectByName.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrVal.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		if ((null != modelNames && modelNames.length() != 0) && (attrValueIdeS != null || attrValueIdeS.size() != 0)) {
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdeS.size(); i++) {
				attrValueNameS.add(attrValueIdeS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName_a = goodsModelAttrValueService.selectByName(attrValueNameS,goods.getShopId());
			}
			if (selectByName_a != null || selectByName_a.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName_a) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr_a.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr_a.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrVal_a.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName_a.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		if ((null != modelNamees && modelNamees.length() != 0)
				&& (attrValueIdesS != null || attrValueIdesS.size() != 0)) {
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdesS.size(); i++) {
				attrValueNameS.add(attrValueIdesS.get(i));
			}
			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName_b = goodsModelAttrValueService.selectByName(attrValueNameS,goods.getShopId());
			}
			if (selectByName_b != null || selectByName_b.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName_b) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr_b.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr_b.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrVal_b.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName_b.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		List<Integer> goodsGroups = (List<Integer>) map.get("shopGrouptionsVal");
		if (goodsGroups.size() != 0 || goodsGroups != null) {
			for (Integer groupVal : goodsGroups) {
				ShopGoodsGroupGoods shopGoodsGroupGoods = new ShopGoodsGroupGoods();

				shopGoodsGroupGoods.setGroupId(groupVal);
				shopGoodsGroupGoods.setGroupGoodsStatus(1);
				shopGoodsGroupGoods.setGoodsId(goods.getGoodsId());
				shopGoodsGroupGoods.setCreateUserId(jwtInfo.getId());
				shopGoodsGroupGoods.setCreateTime(new Date());
				shopGoodsGroupGoods.setShopId(goods.getShopId());
				shopGoodsGroupGoods.setMerchantId(1);
				shopGoodsGroupGoodsBiz.insertGroup(shopGoodsGroupGoods);
			}
		}

		for (String img : imgsList) {
			ShopGoodsImg goodsImg = new ShopGoodsImg();
			goodsImg.setCreateTime(new Date());
			goodsImg.setCreateUserId(jwtInfo.getId());
			goodsImg.setGoodsId(goods.getGoodsId());
			goodsImg.setImagePath(img);
			goodsImg.setTitle(goods.getName());
			shopGoodsImgBiz.insertSelective(goodsImg);
		}

		List<Integer> stockQuantity = (List<Integer>) map.get("stockQuantity");

		int stockIndex = 0;
		if ((attrValueIdS != null && attrValueIdS.size() != 0) && (attrValueIdeS != null && attrValueIdeS.size() != 0)
				&& (attrValueIdesS != null && attrValueIdesS.size() != 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_a = new ArrayList<Integer>();
//			if (selectByName_a != null && selectByName_a.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_a) {
//					goodsSpce_a.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_b = new ArrayList<Integer>();
//			if (selectByName_b != null && selectByName_b.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_b) {
//					goodsSpce_b.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//			
			
			
			for (int i = 0; i < goodsAttrVal.size(); i++) {
				for (int j = 0; j < goodsAttrVal_a.size(); j++) {
					for (int k = 0; k < goodsAttrVal_b.size(); k++) {
						ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
						shopGoodsSpec.setGoodsId(goods.getGoodsId());
						shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
						shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
						shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
						shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
						stockIndex++;
						shopGoodsSpec.setMerchantId(1);
						shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
						shopGoodsSpec.setGoodsAttrValueIds(
								goodsAttrVal.get(i) + "," + goodsAttrVal_a.get(j) + "," + goodsAttrVal_b.get(k));
						shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_a.get(j)+"+"+goodsAttrValName_b.get(k));
						shopGoodsSpecBiz.add(shopGoodsSpec);
					}
				}
			}
		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS != null && attrValueIdeS.size() != 0)
				&& (attrValueIdesS == null || attrValueIdesS.size() == 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_a = new ArrayList<Integer>();
//			if (selectByName_a != null && selectByName_a.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_a) {
//					goodsSpce_a.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			
			
			for (int i = 0; i < goodsAttrVal.size(); i++) {
				for (int j = 0; j < goodsAttrVal_a.size(); j++) {
					ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
					shopGoodsSpec.setGoodsId(goods.getGoodsId());
					shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
					shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
					shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
					shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
					stockIndex++;
					shopGoodsSpec.setMerchantId(1);
					shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
					shopGoodsSpec.setGoodsAttrValueIds(goodsAttrVal.get(i) + "," + goodsAttrVal_a.get(j));
					shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_a.get(j));
					shopGoodsSpecBiz.add(shopGoodsSpec);
				}

			}
		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS == null || attrValueIdeS.size() == 0)
				&& (attrValueIdesS != null && attrValueIdesS.size() != 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_b = new ArrayList<Integer>();
//			if (selectByName_b != null && selectByName_b.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_b) {
//					goodsSpce_b.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			
			for (int i = 0; i < goodsAttrVal.size(); i++) {
				for (int k = 0; k < goodsAttrVal_b.size(); k++) {
					ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
					shopGoodsSpec.setGoodsId(goods.getGoodsId());
					shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
					shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
					shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
					shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
					stockIndex++;
					shopGoodsSpec.setMerchantId(1);
					shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
					shopGoodsSpec.setGoodsAttrValueIds(goodsAttrVal.get(i) + "," + goodsAttrVal_b.get(k));
					shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_b.get(k));
					shopGoodsSpecBiz.add(shopGoodsSpec);
				}
			}

		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS == null || attrValueIdeS.size() == 0)
				&& (attrValueIdesS == null || attrValueIdesS.size() == 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			
			for (int i = 0; i < goodsAttrVal.size(); i++) {
				ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
				shopGoodsSpec.setGoodsId(goods.getGoodsId());
				shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
				shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
				shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
				shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
				stockIndex++;
				shopGoodsSpec.setMerchantId(1);
				shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
				shopGoodsSpec.setGoodsAttrValueIds(String.valueOf(goodsAttrVal.get(i)));
				shopGoodsSpec.setName(goodsAttrValName.get(i));
				shopGoodsSpecBiz.add(shopGoodsSpec);

			}
		}
	}

	@Override
	@Transactional
	public void updateGoods(String token, Map map) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		// 验证权限
		int userType = jwtInfo.getType();

		List<Object> oringinalPrice = (List<Object>) map.get("price");
		List<Integer> oringinalPriceInteger = new ArrayList<Integer>();
		Integer oringinalPriceEs = null;
		Integer oringinalPriceGoods = null;
		if (oringinalPrice != null || oringinalPrice.size() != 0) {
			for (Object str : oringinalPrice) {
				double parseDouble = Double.parseDouble(str.toString());
				oringinalPriceEs = (int) (parseDouble * 100);
				oringinalPriceInteger.add(oringinalPriceEs);
			}
			oringinalPriceGoods = oringinalPriceInteger.get(0);
			for (int i = 0; i < oringinalPriceInteger.size(); i++) {
				if (oringinalPriceGoods > oringinalPriceInteger.get(i)) {
					oringinalPriceGoods = oringinalPriceInteger.get(i);
				}
			}
		}
		List<Object> currentPrice = (List<Object>) map.get("memberPrice");
		List<Integer> currentPriceInteger = new ArrayList<Integer>();
		Integer currentPriceEs = null;
		Integer currentPriceGoods = null;
		if (currentPrice != null || currentPrice.size() != 0) {
			for (Object integer : currentPrice) {
				double parseDouble = Double.parseDouble(integer.toString());
				currentPriceEs = (int) (parseDouble * 100);
				currentPriceInteger.add(currentPriceEs);
			}
			currentPriceGoods = currentPriceInteger.get(0);
			for (int i = 0; i < currentPriceInteger.size(); i++) {
				if (currentPriceGoods > currentPriceInteger.get(i)) {
					currentPriceGoods = currentPriceInteger.get(i);
				}
			}
		}

		List<Object> menchantPrice = (List<Object>) map.get("menchantPrice");
		List<Integer> menchantPriceInteger = new ArrayList<Integer>();
		Integer menchantPriceEs = null;
		if (menchantPrice != null || menchantPrice.size() != 0) {
			for (Object integer : menchantPrice) {
				double parseDouble = Double.parseDouble(integer.toString());
				menchantPriceEs = (int) (parseDouble * 100);
				menchantPriceInteger.add(menchantPriceEs);
			}
		}

		/*
		 * ShopGoods goods = (ShopGoods) BeanUtils.mapToObject(map, ShopGoods.class);
		 */
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(date);
		Date datee = new Date();
		datee = df.parse(format);
		ShopGoods goods = new ShopGoods();
		String  goodsNature_p= (String)map.get("goodsNature");
		for (Object obj : map.keySet()) {
			goods.setGoodsId((Integer) map.get("goodsId"));
			goods.setTitle((String) map.get("title"));
			goods.setSimpleTitle((String) map.get("simpleTitle"));
			goods.setName((String) map.get("name"));
			goods.setMkCategoryCode((String) map.get("mkCategoryCode"));
			goods.setCategoryName((String) map.get("categoryName"));
			goods.setSalesPromotion((String) map.get("salesPromotion"));
			goods.setModifyTime(datee);
			goods.setStatus(Integer.parseInt(map.get("status").toString()));
			goods.setGoodsNature(Integer.valueOf(goodsNature_p));
			/*
			 * goods.setOringinalPrice((Integer)map.get("oringinalPrice"));
			 * goods.setCurrentPrice((Integer)map.get("currentPrice"));
			 */
			goods.setDescription((String) map.get("description"));
		}
		List<Integer> goodsGroups = (List<Integer>) map.get("shopGrouptionsValForm");
		Integer shopId = (Integer) map.get("shopId");
		if (goodsGroups.size() != 0 || goodsGroups != null) {
			shopGoodsGroupGoodsBiz.deleteShopGoods(goods.getGoodsId());
			for (Integer groupVal : goodsGroups) {
				ShopGoodsGroupGoods shopGoodsGroupGoods = new ShopGoodsGroupGoods();
				shopGoodsGroupGoods.setGroupId(groupVal);
				shopGoodsGroupGoods.setGroupGoodsStatus(1);
				shopGoodsGroupGoods.setGoodsId(goods.getGoodsId());
				shopGoodsGroupGoods.setCreateUserId(jwtInfo.getId());
				shopGoodsGroupGoods.setCreateTime(new Date());
				shopGoodsGroupGoods.setShopId(shopId);
				shopGoodsGroupGoods.setModifyUserId(jwtInfo.getId());
				shopGoodsGroupGoods.setModifyTime(new Date());
				shopGoodsGroupGoods.setMerchantId(1);
				shopGoodsGroupGoodsBiz.insertGroup(shopGoodsGroupGoods);
			}
		}
		ShopGoods searchGoods = shopGoodsBiz.selectByPrimaryKey(goods.getGoodsId());
		if (searchGoods == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品不存在");
		}
		Integer merchantId = null;
		if (userType == JWTInfo.USER_TYPE_MERCHANTT) {
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
			if (searchGoods.getMerchantId() == null || searchGoods.getMerchantId().intValue() != merchantId) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限修改");
			}
		}
		List<String> imgsList = (List<String>) map.get("goodImgs");

		goods.setCheckStatus(1);
		// if(jwtInfo.getType() == JWTInfo.USER_TYPE_ADMIN) {
		//// goods.setShopId(1);
		// goods.setCheckStatus(1);
		// }
		goods.setCurrentPrice(currentPriceGoods);
		goods.setOringinalPrice(oringinalPriceGoods);
		goods.setMemberPrice(currentPriceGoods);
		goods.setStockStatus(1);
		goods.setDeleteStatus(-1);
		// goods.setStatus(1);
		goods.setShowStatus(1);
		goods.setRecommendStatus(1);
		goods.setTitleImage(imgsList.get(0));

		goods.setModifyTime(new Date());
		shopGoodsBiz.updateSelectiveById(goods);

		// 删除原来的图片
		shopGoodsImgBiz.deleteByGoodsId(goods.getGoodsId());

		// 新增图片
		for (String img : imgsList) {
			ShopGoodsImg goodsImg = new ShopGoodsImg();
			goodsImg.setCreateTime(new Date());
			goodsImg.setCreateUserId(jwtInfo.getId());
			goodsImg.setGoodsId(goods.getGoodsId());
			goodsImg.setImagePath(img);
			goodsImg.setTitle(goods.getName());
			shopGoodsImgBiz.insertSelective(goodsImg);
		}
		// --------------------------------------------------
		//删除原来的商品规格
		goodsAttrService.deleteGoodsAttr(goods.getGoodsId());
		//删除原来的商品规格值
		goodsAttrValueService.deleteGoodsAttrValue(goods.getGoodsId());
		String modelName = (String) map.get("modelAttrId");
		String modelNames = (String) map.get("modelAttrIde");
		String modelNamees = (String) map.get("modelAttrIdes");
		ShopGoodsModelAttr shopGoodsModelAttr = new ShopGoodsModelAttr();
		ShopGoodsModelAttr shopGoodsModelAttr_a = new ShopGoodsModelAttr();
		ShopGoodsModelAttr shopGoodsModelAttr_b = new ShopGoodsModelAttr();

		ShopGoodsAttr shopGoodsAttr = new ShopGoodsAttr();
		ShopGoodsAttr shopGoodsAttr_a = new ShopGoodsAttr();
		ShopGoodsAttr shopGoodsAttr_b = new ShopGoodsAttr();
		List<ShopGoodsAttr> ShopGoodsAttrList = new ArrayList<ShopGoodsAttr>();
		if (null != modelName && modelName.length() != 0) {
			shopGoodsModelAttr = goodsModelAttrService.selectByName(modelName,shopId);
			shopGoodsAttr.setGoodsId(goods.getGoodsId());
			shopGoodsAttr.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
			shopGoodsAttr.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
			shopGoodsAttr.setShopId(shopGoodsModelAttr.getShopId());
			shopGoodsAttr.setCreateTime(new Date());
			shopGoodsAttr.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr);
		}
		if (null != modelNames && modelNames.length() != 0) {
			shopGoodsModelAttr_a = goodsModelAttrService.selectByName(modelNames,shopId);
			shopGoodsAttr_a.setGoodsId(goods.getGoodsId());
			shopGoodsAttr_a.setModelAttrId(shopGoodsModelAttr_a.getModelAttrId());
			shopGoodsAttr_a.setModelAttrName(shopGoodsModelAttr_a.getModelAttrName());
			shopGoodsAttr_a.setShopId(shopGoodsModelAttr_a.getShopId());
			shopGoodsAttr_a.setCreateTime(new Date());
			shopGoodsAttr_a.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr_a.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr_a);
		}
		if (null != modelNamees && modelNamees.length() != 0) {
			shopGoodsModelAttr_b = goodsModelAttrService.selectByName(modelNamees,shopId);
			shopGoodsAttr_b.setGoodsId(goods.getGoodsId());
			shopGoodsAttr_b.setModelAttrId(shopGoodsModelAttr_b.getModelAttrId());
			shopGoodsAttr_b.setModelAttrName(shopGoodsModelAttr_b.getModelAttrName());
			shopGoodsAttr_b.setShopId(shopGoodsModelAttr_b.getShopId());
			shopGoodsAttr_b.setCreateTime(new Date());
			shopGoodsAttr_b.setCreateUserId(jwtInfo.getId());
			shopGoodsAttr_b.setMerchantId(1);
			ShopGoodsAttrList.add(shopGoodsAttr_b);
		}

		if (ShopGoodsAttrList != null || ShopGoodsAttrList.size() != 0) {
			for (ShopGoodsAttr goodsAttr : ShopGoodsAttrList) {
				goodsAttrService.insertGoodsAttr(goodsAttr);
			}
		}

		List<String> attrValueIdS = (List<String>) map.get("attrValueId");
		List<String> attrValueIdeS = (List<String>) map.get("attrValueIde");
		List<String> attrValueIdesS = (List<String>) map.get("attrValueIdes");

		List<String> imgsSpecList = (List<String>) map.get("goodsSpecImgs");

		List<ShopGoodsModelAttrValue> selectByName = null;
		List<ShopGoodsModelAttrValue> selectByName_a = null;
		List<ShopGoodsModelAttrValue> selectByName_b = null;
		
		List<Integer> goodsAttrValue=new ArrayList<Integer>();
		List<Integer> goodsAttrValue_a=new ArrayList<Integer>();
		List<Integer> goodsAttrValue_b=new ArrayList<Integer>();
		
		List<String> goodsAttrValName=new ArrayList<String>();
		List<String> goodsAttrValName_a=new ArrayList<String>();
		List<String> goodsAttrValName_b=new ArrayList<String>();
		
		if ((null != modelName && modelName.length() != 0) && (attrValueIdS != null || attrValueIdS.size() != 0)
				&& (imgsSpecList != null && imgsSpecList.size() != 0)) { // 有图片
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdS.size(); i++) {
				attrValueNameS.add(attrValueIdS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName = goodsModelAttrValueService.selectByName(attrValueNameS,shopId);
			}
			int i = 0;
			if (selectByName != null || selectByName.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					shopGoodsAttrValue.setImageUrl(imgsSpecList.get(i));
					i++;
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrValue.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName.add(goodsModelAttr.getAttrValueName());
				}
			}

		} else if ((null != modelName && modelName.length() != 0) && (attrValueIdS != null || attrValueIdS.size() != 0)
				&& (imgsSpecList == null || imgsSpecList.size() == 0)) { // 没有图片
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdS.size(); i++) {
				attrValueNameS.add(attrValueIdS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName = goodsModelAttrValueService.selectByName(attrValueNameS,shopId);
			}
			if (selectByName != null || selectByName.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrValue.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		if ((null != modelNames && modelNames.length() != 0) && (attrValueIdeS != null || attrValueIdeS.size() != 0)) {
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdeS.size(); i++) {
				attrValueNameS.add(attrValueIdeS.get(i));
			}

			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName_a = goodsModelAttrValueService.selectByName(attrValueNameS,shopId);
			}
			if (selectByName_a != null || selectByName_a.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName_a) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr_a.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr_a.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrValue_a.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName_a.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		if ((null != modelNamees && modelNamees.length() != 0)
				&& (attrValueIdesS != null || attrValueIdesS.size() != 0)) {
			List<String> attrValueNameS = new ArrayList<String>();
			for (int i = 0; i < attrValueIdesS.size(); i++) {
				attrValueNameS.add(attrValueIdesS.get(i));
			}
			if (attrValueNameS != null || attrValueNameS.size() != 0) {
				selectByName_b = goodsModelAttrValueService.selectByName(attrValueNameS,shopId);
			}
			if (selectByName_b != null || selectByName_b.size() != 0) {
				for (ShopGoodsModelAttrValue goodsModelAttr : selectByName_b) {
					ShopGoodsAttrValue shopGoodsAttrValue = new ShopGoodsAttrValue();
					shopGoodsAttrValue.setGoodsId(goods.getGoodsId());
					shopGoodsAttrValue.setModelAttrId(shopGoodsModelAttr_b.getModelAttrId());
					shopGoodsAttrValue.setModelAttrName(shopGoodsModelAttr_b.getModelAttrName());
					shopGoodsAttrValue.setModelAttrValueId(goodsModelAttr.getAttrValueId());
					shopGoodsAttrValue.setModelAttrValueName(goodsModelAttr.getAttrValueName());
					shopGoodsAttrValue.setShopId(goodsModelAttr.getShopId());
					shopGoodsAttrValue.setMerchantId(1);
					shopGoodsAttrValue.setCreateUserId(jwtInfo.getId());
					shopGoodsAttrValue.setCreateTime(new Date());
					goodsAttrValueService.addGoodsAttrValue(shopGoodsAttrValue);
					goodsAttrValue_b.add(shopGoodsAttrValue.getGoodsAttrValueId());
					goodsAttrValName_b.add(goodsModelAttr.getAttrValueName());
				}
			}
		}

		// 删除原来的规格
		// shopGoodsSpecBiz.deleteByGoodsId(goods.getGoodsId());
		List<ShopGoodsSpec> goodsSpecList = shopGoodsSpecBiz.selectListByGoodsId(goods.getGoodsId());
		for (ShopGoodsSpec spec : goodsSpecList) {
			shopGoodsSpecBiz.delete(spec);
		}

		List<Integer> stockQuantity = (List<Integer>) map.get("stockQuantity");

		int stockIndex = 0;
		if ((attrValueIdS != null && attrValueIdS.size() != 0) && (attrValueIdeS != null && attrValueIdeS.size() != 0)
				&& (attrValueIdesS != null && attrValueIdesS.size() != 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_a = new ArrayList<Integer>();
//			if (selectByName_a != null && selectByName_a.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_a) {
//					goodsSpce_a.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_b = new ArrayList<Integer>();
//			if (selectByName_b != null && selectByName_b.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_b) {
//					goodsSpce_b.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			
			for (int i = 0; i < goodsAttrValue.size(); i++) {
				for (int j = 0; j < goodsAttrValue_a.size(); j++) {
					for (int k = 0; k < goodsAttrValue_b.size(); k++) {
						ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
						shopGoodsSpec.setGoodsId(goods.getGoodsId());
						shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
						shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
						shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
						shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
						stockIndex++;
						shopGoodsSpec.setMerchantId(1);
						shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
						shopGoodsSpec.setGoodsAttrValueIds(
								goodsAttrValue.get(i) + "," + goodsAttrValue_a.get(j) + "," + goodsAttrValue_b.get(k));
						shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_a.get(j)+"+"+goodsAttrValName_b.get(k));
						shopGoodsSpecBiz.add(shopGoodsSpec);
					}
				}
			}
		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS != null && attrValueIdeS.size() != 0)
				&& (attrValueIdesS == null || attrValueIdesS.size() == 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_a = new ArrayList<Integer>();
//			if (selectByName_a != null && selectByName_a.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_a) {
//					goodsSpce_a.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
		
			for (int i = 0; i < goodsAttrValue.size(); i++) {
				for (int j = 0; j < goodsAttrValue_a.size(); j++) {
					ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
					shopGoodsSpec.setGoodsId(goods.getGoodsId());
					shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
					shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
					shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
					shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
					stockIndex++;
					shopGoodsSpec.setMerchantId(1);
					shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
					shopGoodsSpec.setGoodsAttrValueIds(goodsAttrValue.get(i) + "," + goodsAttrValue_a.get(j));
					shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_a.get(j));
					shopGoodsSpecBiz.add(shopGoodsSpec);
				}

			}
		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS == null || attrValueIdeS.size() == 0)
				&& (attrValueIdesS != null && attrValueIdesS.size() != 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
//
//			List<Integer> goodsSpce_b = new ArrayList<Integer>();
//			if (selectByName_b != null && selectByName_b.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName_b) {
//					goodsSpce_b.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			for (int i = 0; i < goodsAttrValue.size(); i++) {
				for (int k = 0; k < goodsAttrValue_b.size(); k++) {
					ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
					shopGoodsSpec.setGoodsId(goods.getGoodsId());
					shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
					shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
					shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
					shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
					stockIndex++;
					shopGoodsSpec.setMerchantId(1);
					shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
					shopGoodsSpec.setGoodsAttrValueIds(goodsAttrValue.get(i) + "," + goodsAttrValue_b.get(k));
					shopGoodsSpec.setName(goodsAttrValName.get(i)+"+"+goodsAttrValName_b.get(k));
					shopGoodsSpecBiz.add(shopGoodsSpec);
				}
			}

		} else if ((attrValueIdS != null && attrValueIdS.size() != 0)
				&& (attrValueIdeS == null || attrValueIdeS.size() == 0)
				&& (attrValueIdesS == null || attrValueIdesS.size() == 0)) {
//			List<Integer> goodsSpce = new ArrayList<Integer>();
//			if (selectByName != null && selectByName.size() != 0) {
//				for (ShopGoodsModelAttrValue goodsModelAttrValue : selectByName) {
//					goodsSpce.add(goodsModelAttrValue.getAttrValueId());
//				}
//			}
			for (int i = 0; i < goodsAttrValue.size(); i++) {
				ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
				shopGoodsSpec.setGoodsId(goods.getGoodsId());
				shopGoodsSpec.setStockQuantity(stockQuantity.get(stockIndex));
				shopGoodsSpec.setPrice(oringinalPriceInteger.get(stockIndex));
				shopGoodsSpec.setMemberPrice(currentPriceInteger.get(stockIndex));
				shopGoodsSpec.setMenchantPrice(menchantPriceInteger.get(stockIndex));
				stockIndex++;
				shopGoodsSpec.setMerchantId(1);
				shopGoodsSpec.setShopId(shopGoodsModelAttr.getShopId());
				shopGoodsSpec.setGoodsAttrValueIds(String.valueOf(goodsAttrValue.get(i)));
				shopGoodsSpec.setName(goodsAttrValName.get(i));
				shopGoodsSpecBiz.add(shopGoodsSpec);

			}
		}
	}

	@Override
	public PageInfo<ShopGoods> selectPageShopGoodsList(Map<String, Object> map, Integer pageSize, Integer pageNum)
			throws Exception {
		PageHelper.startPage(pageNum, pageSize);
		List<ShopGoods> list = null;
		if (StringUtils.isNotEmpty(map.get("groupId").toString())) {
			list = shopGoodsBiz.selectPageShopGoodsListByGroupId(map);
		} else {
			list = shopGoodsBiz.selectPageShopGoodsListByMap(map);
		}
		PageInfo<ShopGoods> page = new PageInfo<ShopGoods>(list);
		return page;
	}

	@Override
	public GoodsDetailVo selectShopGoodsAdminById(String goodsId) throws Exception {
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();

		// 根据商品ID查出商品
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(Integer.parseInt(goodsId));
		// if (!checkShoppingGoods(shopGoods)) {
		// throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		// }
		// 商品规格
		List<ShopGoodsSpec> shopGoodsSpecList = shopGoodsSpecBiz.selectListByGoodsId(Integer.parseInt(goodsId));
		// 根据商品ID查出图片
		List<ShopGoodsImg> shopGoodsImgList = shopGoodsImgBiz.selectListByGoodsId(Integer.parseInt(goodsId));
		goodsDetailVo.setShopGoods(shopGoods);
		goodsDetailVo.setShopGoodsImgList(shopGoodsImgList);
		goodsDetailVo.setShopGoodsSpecList(shopGoodsSpecList);
		return goodsDetailVo;
	}

	@Override
	public PageInfo<ShopGoodsAdmin> selectPageMerchantShopGoodsAdminList(String token, Map<String, Object> map,
			Integer pageNo, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		if (StringUtils.isEmpty(jwtInfo.getAttr1())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
		}
		map.put("merchantId", Integer.parseInt(jwtInfo.getAttr1()));
		PageHelper.startPage(pageNo, pageSize);
		List<ShopGoodsAdmin> list = shopGoodsBiz.selectPageShopGoodsAdminList(map);
		PageInfo<ShopGoodsAdmin> page = new PageInfo<ShopGoodsAdmin>(list);
		return page;
	}

	@Override
	public void deleteGoods(String token, Integer goodsId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		// 验证权限
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		ShopGoods searchGoods = shopGoodsBiz.selectByPrimaryKey(goodsId);
		if (searchGoods == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品不存在");
		}
		if (userType == JWTInfo.USER_TYPE_MERCHANTT) {
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
			if (searchGoods.getMerchantId() == null || searchGoods.getMerchantId().intValue() != merchantId) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限修改");
			}
		}
		searchGoods.setDeleteStatus(ShopGoods.DELETE_STATUS_YES);
		searchGoods.setModifyTime(new Date());
		shopGoodsBiz.updateSelectiveById(searchGoods);

	}

	@Override
	public List<ShopGoods> selectShopGoodsByShopId(Integer shopId) throws Exception {

		return shopGoodsBiz.selectByShopId(shopId);
	}

	@Override
	public GoodsDetailVo getRechargeGoods(String shopId) throws Exception {
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
		// 查出移动充值产品
		ShopGoods shopGoods = shopGoodsBiz.selectRechargeGoods(shopId);
		if (shopGoods == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品不存在");
		}

		// 商品规格
		List<ShopGoodsSpec> shopGoodsSpecList = shopGoodsSpecBiz.selectListByGoodsId(shopGoods.getGoodsId());
		List<ShopGoodsSpecVo> shopGoodsSpecVoList = new ArrayList<ShopGoodsSpecVo>();
		for (int i = 0; i < shopGoodsSpecList.size(); i++) {
			ShopGoodsSpecVo shopGoodsSpecVo = new ShopGoodsSpecVo();
			org.springframework.beans.BeanUtils.copyProperties(shopGoodsSpecList.get(i), shopGoodsSpecVo);
			shopGoodsSpecVo.setS1("0");
			shopGoodsSpecVo.setS2("0");
			shopGoodsSpecVo.setS3("0");
			if (StringUtils.isNotEmpty(shopGoodsSpecVo.getGoodsAttrValueIds())) {
				String[] ids = shopGoodsSpecVo.getGoodsAttrValueIds().split(",");
				shopGoodsSpecVo.setS1(ids[0]);
				if (ids.length > 1) {
					shopGoodsSpecVo.setS2(ids[1]);
				}
				if (ids.length > 2) {
					shopGoodsSpecVo.setS3(ids[2]);
				}
			}
			shopGoodsSpecVoList.add(shopGoodsSpecVo);
		}

		if (shopGoodsSpecList == null || shopGoodsSpecList.size() == 0) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品规格不存在");
		}

		// 商品对应规格
		List<ShopGoodsAttrVo> shopGoodsAttrList = shopGoodsAttrBiz.selectListByGoodsId(shopGoods.getGoodsId());
		for (int i = 0; i < shopGoodsAttrList.size(); i++) {
			shopGoodsAttrList.get(i).setKeyStr("s" + (i + 1));
		}

		goodsDetailVo.setShopGoods(shopGoods);
		goodsDetailVo.setShopGoodsSpecList(shopGoodsSpecVoList);
		goodsDetailVo.setShopGoodsAttrList(shopGoodsAttrList);
		return goodsDetailVo;
	}
}
